<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	set_html('title','购买会员');
	$basic=$return['pluginset'];
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	$return['post']=$post;
	$return['fee']=price($basic['mfee']);
	$return['fee0']=$basic['mfee0']?price($basic['mfee0']):price($basic['mfee']*1.5);
	$return['vipbg']=$basic['vipbg'];
	$return['basic']=$basic;
	exi($return);
}
if($_RQ['op']=='post'){
	$basic=$return['pluginset'];
		$postdata=get_postdata();
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		if($packname==1){
			$fee=$basic['mfee']*$packnum;
		}elseif($packname==2){
			$fee=$basic['jfee']*$packnum;	
		}elseif($packname==3){
			$fee=$basic['yfee']*$packnum;
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_lineks_viporder','ordersn'),
			'fee'=>$fee,
			'packname'=>$packname,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_lineks_viporder',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买会员订单';
		$params['id']=$id;
		$params['plugin']='jy_lineks';
		//$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		//params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_lineks/buyvip/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
		exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_lineks_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_lineks_viporder',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_lineks_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['vipendtime']!=0){
		$addTime=$user['vipendtime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_lineks_member',array('isvip'=>1,'vipendtime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('commission')->jy_commission($order);
	exi('支付成功','success','jy_lineks/myhome/main/');
}